package cn.achao.csr.openapi.core.model.resp;

import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;

/**
 * @author achao
 * @description 响应
 * @date 2023/10/26 9:48
 */
@SuperBuilder
@Data
@NoArgsConstructor
public class ThirdResponse {

    /**
     * 成功编码
     */
    public static String SUC = "0";

    /**
     * 失败编码
     */
    public static String ERR = "1";

    /**
     * 超时
     **/
    public static String TIME_OUT = "2";

    @Builder.Default
    public String oRet = SUC;

    public String msg;

    public ThirdResponse(String oRet) {
        this.oRet = oRet;
    }

    public void setSuccess() {
        oRet = SUC;
    }

    public void setErr() {
        oRet = ERR;
    }

    public void setTimeOut(){
        oRet = TIME_OUT;
    }

    public boolean isSuccess() {
        return SUC.equals(oRet);
    }

    public boolean isTimeOut(){
        return TIME_OUT.equals(oRet);
    }

    public static <T extends ThirdResponse> T suc(Class<T> clz) {

        T t = null;
        try {
            t = clz.newInstance();
            t.setSuccess();
        } catch (InstantiationException | IllegalAccessException e) {
            throw new RuntimeException(e);
        }
        return t;

    }

    public static ThirdResponse fail() {
        ThirdResponse resp = new ThirdResponse();
        resp.setErr();
        return resp;
    }

    public static <T extends ThirdResponse> T fail(Class<T> clz, String msg) {

        T t;
        try {
            t = clz.newInstance();
            t.setErr();
            t.msg = msg;
        } catch (InstantiationException | IllegalAccessException e) {
            throw new RuntimeException(e);
        }
        return t;

    }

    public boolean after() {
        return true;
    }
}
